package com.example.online_order.service.impl;

import com.example.online_order.Eception.BussinessException;
import com.example.online_order.Eception.ExceptionType;

import com.example.online_order.mapper.ShoucanglanMapper;

import com.example.online_order.pojo.AdminAccount;
import com.example.online_order.pojo.Desk;
import com.example.online_order.pojo.Shoucanglan;
import com.example.online_order.service.ShoucanglanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class ShoucanglanServiceimpl implements ShoucanglanService {
    @Autowired
    ShoucanglanMapper mapper;

    @Transactional
    @Override
    public Integer addToS(Shoucanglan s) {
        Shoucanglan sDb = mapper.getSByGoodsId(s.getUserId(),s.getGoodsId());
        if (sDb != null){
            throw new BussinessException(ExceptionType.bus_id_exists,"收藏已存在");
        }else {
            Integer res = mapper.addToS(s);
            if (res != 1){
                throw new BussinessException(ExceptionType.bus_insert_fail,"添加收藏失败");
            }else {
                return res;
            }
        }

    }


    @Transactional
    @Override
    public Integer deleteS(Integer id) {
        Shoucanglan sDb = mapper.getSById(id);
        if (sDb == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"收藏不存在");
        }else {
            Integer res = mapper.deleteS(id);
            if (res!= 1){
                throw new BussinessException(ExceptionType.bus_delete_fail,"删除收藏失败");
            }else {
                return res;
            }
        }

    }


    @Transactional
    @Override
    public Integer updateS(Shoucanglan s) {
        Shoucanglan sDb = mapper.getSById(s.getId());
        if (sDb == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"收藏不存在");
        }else {
            Integer res = mapper.updateS(s);
            if (res!= 1){
                throw new BussinessException(ExceptionType.bus_update_fail,"修改收藏失败");
            }else {
                return res;
            }
        }

    }

    @Override
    public List<Shoucanglan> getAllS() {
        return mapper.getAllS();
    }

    @Override
    public Shoucanglan getSById(Integer id) {
        return mapper.getSById(id);
    }

    @Override
    public List<Shoucanglan> getSByUsername(String userName) {
        return mapper.getSByUsername(userName);
    }

    @Override
    public Shoucanglan findSByGoodsname(String goodsname) {
        Shoucanglan sDb = mapper.findSByGoodsname(goodsname);
        if(sDb == null) {
            throw new BussinessException(ExceptionType.bus_id_not_found,"不存在");
        }else{
            return sDb;
        }
    }

    @Override
    public Shoucanglan findSLikeGoodsname(String goodsname) {
        Shoucanglan sDb = mapper.findSByGoodsname(goodsname);
        if(sDb == null) {
            throw new BussinessException(ExceptionType.bus_id_not_found,"不存在");
        }else{
            return sDb;
        }
    }
}
